import { Component } from '@angular/core';
import { Hero } from '../hero';
import { HEROES } from '../mock-heroes';
import { HeroService } from '../hero.service';

@Component({
  selector: 'app-dashboard',
  templateUrl: './dashboard.component.html',
  styleUrls: ['./dashboard.component.sass']
})
export class DashboardComponent {
  heroes: Hero[] = HEROES

  // 注入HeroService
  constructor(private heroService: HeroService) { }
  // 生命周期钩子,创建完组件后很快就会调用
  ngOnInit() {
    this.getHeroes();
  }
  getHeroes(): void {
    // HttpClient.get  请求服务
    // Observable类通过.subscribe()消费数据(与Promise,then类似)
    this.heroService.getHeroes().subscribe(heroes => this.heroes = heroes.slice(1, 5));
  }
}
